package com.interview.score.controller;

import com.interview.score.common.Result;
import com.interview.score.common.ResultMessage;
import com.interview.score.common.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/examinerLogin")
public class LoginController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private Environment environment;

    @PostMapping("validation")
    public Result validation(@RequestParam("idNumber")String idNumber, @RequestParam("passWord")String passWord) {
        String ExamRoomIdInRedis=(String) redisTemplate.opsForValue().get(environment.getProperty("ExamRoomIdKey"));
        String examinerIdNumbers=environment.getProperty("ExaminerIdNumbersKey")+ "-"+ExamRoomIdInRedis;

        Boolean member = redisTemplate.opsForSet().isMember(examinerIdNumbers, idNumber);
        if (member){
            String idNumberInRedis=ExamRoomIdInRedis+"-"+idNumber;
            String passWordInRedis =(String) redisTemplate.opsForHash().get(idNumberInRedis, environment.getProperty("PassWordField"));
            if (passWordInRedis.equals(passWord)){
                return new Result(true,StatusCode.OK,ResultMessage.OPERATION_SUCCESS,null);
            }else {
                return new Result(false, StatusCode.ERROR, ResultMessage.PassWord_ERROR,null);
            }}else {
            return new Result(false,StatusCode.ERROR,ResultMessage.EXAMINER_NOT_EXIST,null);
        }


    }

}
